package engine.systems.spatial.parallaxScroll
{
	import engine.classes.enumerations.Axis;
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.spatial.spatialOffset.SpatialOffsetData;
	import engine.systems.update.GameSystem;
	
	/**
	 * The Parallax Scroll System moves Entities at different rates to give the illusion of depth between
	 * layers. This moves layers based on the difference between the Entity's Spatial and a target Spatial,
	 * which is usually the Camera's. 
	 */
	public class ParallaxScrollSystem extends GameSystem
	{
		private const PARALLAX_SCROLL:String = "parallax_scroll";
		
		private var data:SpatialOffsetData;
		
		public function ParallaxScrollSystem()
		{
			super(ParallaxScrollNode, updateNode, nodeAdded, nodeRemoved);
			
			this.priority = SystemPriority.UPDATE;
		}
		
		private function updateNode(node:ParallaxScrollNode, time:Number):void
		{
			data = node.offset.get(PARALLAX_SCROLL);
			
			switch(node.scroll.axis)
			{
				case Axis.X:
					data.x = (node.spatial.x + node.scroll.target.x) * node.scroll.rateX + node.scroll.offsetX;
					break;
				
				case Axis.Y:
					data.y = (node.spatial.y + node.scroll.target.y) * node.scroll.rateY + node.scroll.offsetY;
					break;
				
				case Axis.BOTH:
					data.x = (node.spatial.x + node.scroll.target.x) * node.scroll.rateX + node.scroll.offsetX;
					data.y = (node.spatial.y + node.scroll.target.y) * node.scroll.rateY + node.scroll.offsetY;
					break;
			}
		}
		
		private function nodeAdded(node:ParallaxScrollNode):void
		{
			node.offset.add(PARALLAX_SCROLL, new SpatialOffsetData());
		}
		
		private function nodeRemoved(node:ParallaxScrollNode):void
		{
			node.offset.remove(PARALLAX_SCROLL);
		}
	}
}